<?php

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

// use Illuminate\Http\Request;
// use App\Models\Authorization;
// use App\Transformers\AuthorizationTransformer;

class ColistController extends BaseController
{
    /**
     * 获取首页数据
     * @get("/{spid}/collist/{collistid}/index")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    // 首页栏目列表的ID->id
    public function index(Request $request, $spid, $termid, $collistid)
    {
        // 首页栏目数据
        $collists = DB::table('collists')->where('collistid', $collistid)->first();
        // 首页
        if (empty($collists)) {
            return response()->json(['status' => '404', 'data' => '首页栏目数据为空!']);
        }
   
        // 获取所有的栏目 默认升序,还可以降序desc
        $allColumns = DB::table('columns')->orderBy('listorder')->get();
        $indexColumns = [];
        foreach ($allColumns as $value) {
            if (!empty(json_decode($value->collistid,TRUE))) {
                if (in_array($collistid, json_decode($value->collistid,TRUE))) {
                    $indexColumns[] = $value;
                }
            }
        }

        $data    = [];
        $datas   = [];
        foreach ($indexColumns as $key => $value) {
            // 根据栏目id查找对应二级栏目Id
            // $column               = DB::table('columns')->where('columnid', $value->id)->first();
            $data['rowid']    = $value->id;
            $data['title']    = $value->title;
            $data['subtitle'] = $value->subtitle;
            $data['img']      = $value->img;
            // 栏目模板类型
            $template_type = $value->template_type;
            // 栏目模板编号
            $templateNo = $value->template_no;
            // 栏目列表并没有归类
            $data['tags']                        = '';
            $data['onfocus']['route']['tplType'] = $template_type;
            $data['onfocus']['route']['tplID']   = $templateNo;
            $data['onfocus']['href']             = url("{$spid}/{$termid}/area/template_type/area/template_no/{$templateNo}");
            $data['onclick']['route']['tplType'] = $template_type;
            $data['onclick']['route']['tplID']   = $templateNo;
            $data['onclick']['href']             = url("{$spid}/{$termid}/area/template_type/area/template_no/{$templateNo}");

            $datas[] = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取指定栏目的数据
     * @get("{spid}/column/{columnid}/meta")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function columnsIndex(Request $request, $spid, $termid, $collistid, $columnid)
    {
        // $termid = $request->header('device-token');
        // 设备ID
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID不能为空!']);
        }
        if (empty($collistid)) {
            return response()->json(['status' => '404', 'data' => '栏目列表ID不能为空!']);
        }
        if (empty($columnid)) {
            return response()->json(['status' => '404', 'data' => '栏目ID不能为空!']);
        }
        // 查询指定栏目列表指定栏目的推荐位
        $recommend = DB::table('recommends')->where(['collist_id' => $collistid, 'column_id' => $columnid])->first();
        if (empty($recommend)) {
            return response()->json(['status' => '404', 'data' => '该栏目下无推荐位信息!']);
        }
        // 取出此栏目下面关联的媒资信息
        $vassetis = json_decode($recommend->vassetis, 1);
        if (empty($vassetis)) {
            return response()->json(['status' => '404', 'data' => '该推荐位无关联媒资信息!']);
        }
        $data  = [];
        $datas = [];
        foreach ($vassetis as $vasseti_key => $vasseti_value) {
            // 根据媒资里面的ID取到相关媒资数据
            $vasseti = DB::table('vassetis')->where(['vasseti_id' => $vasseti_value['id']])->first();
            if (!empty($vasseti)) {
                // 取出相关媒资信息
                $data['rowid']    = $vasseti->vasseti_id;
                $data['img']      = json_decode($vasseti->imgs, 1);
                $data['title']    = $vasseti->title;
                $data['subtitle'] = $vasseti->subtitle;
               
                $data['tags'] = DB::table('vasseti_vcattags')->where('vasseti_vcattags.vasseti_id', $vasseti->vasseti_id)->join('vcattags', 'vasseti_vcattags.vcattag_id', '=', 'vcattags.vcattag_id')->pluck('vcattags.name')->implode(',', 'name');

                $data['onfocus']['route']['tplType'] = '';
                $data['onfocus']['route']['tplID']   = '';
                $data['onfocus']['href']             = url("/{$spid}/{$termid}/vasset/{$vasseti->vasseti_id}/meta");
                $data['onclick']['route']['tplType'] = '';
                $data['onclick']['route']['tplID']   = '';
                $data['onclick']['href']             = url("/{$spid}/{$termid}/vasset/{$vasseti->vasseti_id}/meta");
                $datas[]                             = $data;
            }
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取指定服务商栏目列表
     * @get("{spid}/collist")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function collistIndex(Request $request, $spid, $termid)
    {
        // 获取所有的栏目列表
        $collists = DB::table('collists')->get();
        $data     = [];
        $datas    = [];
        foreach ($collists as $key => $value) {
            $data['rowid'] = $value->collistid;
            // $data['collistid']  = $value->collistid;
            $data['collist_no']                  = $value->collist_no;
            $columns                             = json_decode($value->columns);
            $data['columns']                     = $columns;
            $data['img']                         = $value->img;
            $data['title']                       = $value->title;
            $data['subtitle']                    = $value->subtitle;
            $data['tags']                        = '';
            $data['short_desc']                  = $value->short_desc;
            $data['long_desc']                   = $value->long_desc;

            $data['onfocus']['route']['tplType'] = 'AREA';
            $templateNo                          = '00' . ($key + 1);
            $data['onfocus']['route']['tplID']   = $templateNo;
            ///{spid}/collist/{collistid}/meta
            $data['onfocus']['href']             = url("{$spid}/{$termid}/area/template_type/area/template_no/{$templateNo}");
            $data['onclick']['route']['tplType'] = 'AREA';
            $data['onclick']['route']['tplID']   = '00' . ($key + 1);
            $data['onclick']['href']             = url("{$spid}/{$termid}/collist/{$value->collistid}/meta");
            $datas[]                             = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取指定服务商指定列表下的联动列表
     * @get("{spid}/column/{columnid}/meta")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function metaIndex(Request $request, $spid, $termid, $collistid)
    {
        // 指定栏目列表数据
        $collists = DB::table('collists')->where('collistid', $collistid)->first();
        if (empty($termid)) {
            return response()->json(['status' => '404', 'data' => '设备ID为空!']);
        }
        // 首页
        if (empty($collists)) {
            return response()->json(['status' => '404', 'data' => '栏目列表数据为空!']);
        }
        // 获取所有的栏目 默认升序,还可以降序desc
        $allColumns = DB::table('columns')->orderBy('listorder')->get();
        $columns = [];
        foreach ($allColumns as $value) {
            if (!empty(json_decode($value->collistid,TRUE))) {
                if (in_array($collistid, json_decode($value->collistid,TRUE))) {
                    $columns[] = $value;
                }
            }
        }
        $data    = [];
        $datas   = [];
        foreach ($columns as $key => $value) {
            // 根据栏目id查找对应二级栏目Id
            // $column               = DB::table('columns')->where('columnid', $value->id)->first();
            $data[$key]['rowid']    = $value->id;
            $data[$key]['title']    = $value->title;
            $data[$key]['subtitle'] = $value->subtitle;
            $data[$key]['img']      = $value->img;
            // 栏目模板类型
            $template_type = $value->template_type;
            // 栏目模板编号
            $template_no = $value->template_no;
            // 栏目列表并没有归类
            $data[$key]['tags']                        = '';
            $data[$key]['onfocus']['route']['tplType'] = $template_type;
            $data[$key]['onfocus']['route']['tplID']   = $template_no;
            $data[$key]['onfocus']['href']             = url("{$spid}/{$termid}/area/template_type/area/template_no/{$template_no}");
            $data[$key]['onclick']['route']['tplType'] = $template_type;
            $data[$key]['onclick']['route']['tplID']   = $template_no;
            $data[$key]['onclick']['href']             = url("{$spid}/{$termid}/area/template_type/area/template_no/{$template_no}");

        }

        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 获取指定服务商指定栏目列表信息
     * @get("{spid}/column/{columnid}/meta")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function collistMeta(Request $request, $spid, $collist_id)
    {
        // 栏目列表ID
        if (empty($collist_id)) {
            return response()->json(['status' => '404', 'data' => '栏目列表不能为空!']);
        }
        $collist = DB::table('collists')->where(['collistid' => $collist_id])->first();
        if (empty($collist)) {
            return response()->json(['status' => '404', 'data' => '无此栏目列表!']);
        }
        $data['rowid']      = $collist_value->collistid;
        $data['collist_no'] = $collist_value->collist_no;
        $data['name']       = $collist_value->name;
        $data['title']      = $collist_value->title;
        $data['subtitle']   = $collist_value->subtitle;
        $data['short_desc'] = $collist_value->short_desc;
        $data['long_desc']  = $collist_value->long_desc;
        return response()->json(['status' => '200', 'data' => $data]);
    }

    /**
     * 获取指定服务商指定栏目列表的栏目数据
     * @get("{spid}/collist/{collists}/column")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function columnMeta(Request $request, $spid, $collist_id)
    {
        // 栏目列表ID
        if (empty($collist_id)) {
            return response()->json(['status' => '404', 'data' => '栏目列表不能为空!']);
        }
        $collist = DB::table('collists')->where(['collistid' => $collist_id])->first();
        if (empty($collist)) {
            return response()->json(['status' => '404', 'data' => '无此栏目列表!']);
        }
        // 获取栏目列表的栏目信息
        $column = json_decode($collist->column, 1);
        if (empty($column)) {
            return response()->json(['status' => '404', 'data' => '栏目列表下无栏目信息!']);
        }
        $data  = [];
        $datas = [];
        foreach ($column as $column_key => $columnValue) {
            $data['rowid']      = $columnValue['id'];
            $data['column_no']  = $columnValue['column_no'];
            $data['name']       = $columnValue['name'];
            $data['title']      = $columnValue['title'];
            $data['subtitle']   = $columnValue['subtitle'];
            $data['img']        = $columnValue['img'];
            $data['short_desc'] = $columnValue['short_desc'];
            $data['long_desc']  = $columnValue['long_desc'];
            $datas[]            = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

    /**
     * 获取指定服务商指定栏目信息
     * @get("{spid}/column/{columnid}/meta")
     * @Versions({"v1"})
     * @Request()
     * @Response("status": 200, "data":{},"route":{}})
     */
    public function columnSubCollistMeta(Request $request, $spid, $column_id)
    {
        // 栏目ID
        if (empty($column_id)) {
            return response()->json(['status' => '404', 'data' => '栏目不能为空!']);
        }
        $column = DB::table('columns')->where(['columnid' => $column_id])->first();
        if (empty($column)) {
            return response()->json(['status' => '404', 'data' => '无此栏目列表!']);
        }
        $subcolumn = json_decode($column->subcolumns, 1);
        if (empty($subcolumn)) {
            return response()->json(['status' => '404', 'data' => '栏目下无二级栏目列表信息!']);
        }
        $data  = [];
        $datas = [];
        foreach ($subcolumn as $subcolumn_key => $subcolumn_value) {
            $data['rowid']      = $column->columnid;
            $data['collist_no'] = $column->column_no;
            $data['name']       = $column->name;
            $data['title']      = $column->title;
            $data['subtitle']   = $column->subtitle;
            $data['short_desc'] = $column->short_desc;
            $data['long_desc']  = $column->long_desc;
            $datas[]            = $data;
        }
        return response()->json(['status' => '200', 'data' => $datas]);
    }

}
